package Backtracking;

import java.util.ArrayList;
import java.util.List;

/*
*
* 46. 全排列
*
* */
public class Permute46 {
    List<List<Integer>> res=new ArrayList<>();
    public List<List<Integer>> permute(int[] nums) {
        int n=nums.length;
        boolean[] visited=new boolean[n];
        dfs(nums,new ArrayList<>(),visited,n);
        return res;
    }

    private void dfs(int[] nums, ArrayList<Integer> list, boolean[] visited, int n) {
        if(list.size()==n){
            res.add(new ArrayList<>(list));
            return;
        }
        for (int i=0;i<n;i++) {
            if(visited[i]==true){
                continue;
            }else{
                visited[i]=true;
                list.add(nums[i]);
                dfs(nums, list, visited, n);
                visited[i]=false;
                list.remove(list.size()-1);
            }

        }
    }
}
